<?
 require("../inc/passwords.php");

function arrayEscape($array_to_be_treated, $database_connection)
{
        $arr = array();
        foreach ($array_to_be_treated as $key => $value)
		{
                $arr[$key] = $database_connection->real_escape_string($value);
        }
        return $arr;    
}

function getDB()
{
	global $dbusername;
	global $dbpassword;
	global $dbname;

	$database = new mysqli('localhost', $dbusername, $dbpassword, $dbname);
	
	return $database;	
}

function getUsers()
{
	$database = getDB();
	
	$statement = "SELECT * FROM Customer_Name";

	$results = $database->query($statement);

	$database->close();
	
	return $results;
}

function html_input_treatment($array_before)
{
	$array_after = array();
	foreach ($array_before as $key => $value) 
	{
		$array_after[$key] = htmlentities($value);
	}
	return $array_after;	
}

function curl_get($path, $method="GET", $data=null)
{
	$curl = curl_init();
	curl_setopt($curl, CURLOPT_HEADER, FALSE);
	curl_setopt($curl, CURLOPT_TIMEOUT, 20);
	curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
	curl_setopt($curl, CURLOPT_HTTPHEADER, array('Expect:'));
	curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE); // no echo, just return result
	curl_setopt($curl, CURLOPT_USERAGENT, 'SUNY New Paltz web course');
	if ($method === 'POST') {
		curl_setopt($curl, CURLOPT_POST, TRUE);
		curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data));
	}elseif(!empty($data)){
		if(strpos($path, '?') === false)
			$path .= '?';
		else
			$path .= '&';
		$path .= http_build_query($data);
	}
	//echo $path;
	curl_setopt($curl, CURLOPT_URL, $path);

	return curl_exec($curl);
}

function get_string_before($string, $start)
{
	$string = " ".$string;
	$ini = strpos($string,$start);
	if ($ini == 0) return trim($string);
	return trim(substr($string, 0, $ini));
}

?>